﻿using TradePublish.Common;
 using TradePublish.Exceptions;
using log4net;
using TradePublish.Service;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Text;
using System.Threading;
using TradePublish.Entity;
using TradePublish.Common;
using TradePublish.Service;
using TradePublish.Dao;
using System.Data.OracleClient;
namespace TradePublish.Dao
{
    public class TradePublishDao
    {
        private ILog log = null;

        public TradePublishDao()
        {
            this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        public void deleteById(int id)
        {
            string commandText = "update TRADE_PUBLISH  set OCR_IDENTIFY_SUCCESS = 0 ,HAS_GRASPED = 1,HAS_ANTI_SEARCH = 1,HAS_STATUS_SEARCH = 1 WHERE ID = " + id;
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    dbh.ExecuteNonQuery(commandText );
                    return;
                }
                catch (Exception exception)
                {
                    this.log.Error("设置TRADE_PUBLISH的id=" + id + "为识别失败异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                }  
            }
        }

        public DataSet findClientTask(int length )
        {
            string commandText = string.Concat(new object[] { " select  * from TRADE_PUBLISH WHERE (  HAS_GRASPED = 0  or has_anti_search=0 and category=0) and (CLIENT_TASK_NO is null or CLIENT_TASK_NO=0 ) and  ROWNUM<=" + length + " order by id desc   for update" });
            DataSet set = new DataSet();
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    dbh.BeginTransaction();
                    set = dbh.ExecuteDataSet(commandText, TradePublish.Service.ConnectionState.KeepOpen);
                    if (set.Tables == null ||set.Tables.Count == 0 || set.Tables[0].Rows == null ||set.Tables[0].Rows.Count == 0)
                    {
                        //无数据
                        dbh.CommitTransaction();
                        this.log.Info("读取数据库表TRADE_PUBLISH，获得需要本客户端处理的记录0个.");
                        return set;
                    }
                    string str3 = "";
                    for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                    {
                        str3 = str3 + (i == 0? " ":" or ") +" id="+ set.Tables[0].Rows[i]["ID"] ;
                    }
                    str3 = str3.Substring(0, str3.Length - 1);
                    commandText = string.Concat(new object[] { " update TRADE_PUBLISH set HAS_GRASPED=2,HAS_ANTI_SEARCH =2,HAS_STATUS_SEARCH=2,CLIENT_TASK_NO=", Global.clientNo, " where ", str3 });
                    dbh.ExecuteNonQuery(commandText, TradePublish.Service.ConnectionState.KeepOpen);
                    dbh.CommitTransaction();
                    return set; 
                }
                catch (Exception exception)
                {
                    dbh.RollbackTransaction();
                    this.log.Error("读取数据库表TRADE_PUBLISH，获得需要本客户端处理的记录时发送异常" + commandText, exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }
 
        

        public DataSet findTopUnGraspTMImage(int length )
        { 
            string commandText = string.Concat(new object[] { " select *  from (select  * from TRADE_PUBLISH WHERE  HAS_GRASPED = 1 and HAS_GRASP_IMAGE=0  AND OCR_IDENTIFY_SUCCESS=1  order by  issue deSC )  where ROWNUM<=" + length + " order by ROWNUM asc" });
            DataSet set = new DataSet();
            while (true)
            {
                #region 6点到6点半,8点到18点暂停
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                #endregion
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    dbh.BeginTransaction();
                    set = dbh.ExecuteDataSet(commandText, TradePublish.Service.ConnectionState.KeepOpen);
                   
                    if (set.Tables == null || set.Tables[0].Rows == null || set.Tables[0].Rows.Count == 0)
                    {
                        dbh.CommitTransaction(); 
                        this.log.Info("读取数据库表TRADE_PUBLISH，获得尚未采集图片的商标0个.");
                        return set;
                    }
                    this.log.Info("读取数据库表TRADE_PUBLISH，获得尚未采集图片的商标" + set.Tables[0].Rows.Count + "个.");

                    //有数据。
                    StringBuilder updateSql = new StringBuilder("update TRADE_PUBLISH set HAS_GRASP_IMAGE = 2 where ");
                    for (int i = 0; i < set.Tables[0].Rows.Count; i++)
                    {
                        String id = set.Tables[0].Rows[i]["ID"].ToString(); 
                        if (i != 0)
                        {
                            updateSql.Append(" or ");
                        }
                        updateSql.Append("id=").Append(id);
                    }
                    dbh.ExecuteNonQuery(updateSql.ToString(), TradePublish.Service.ConnectionState.KeepOpen);
                    dbh.CommitTransaction(); 
                    return set;
                }
                catch (Exception exception)
                {
                    dbh.RollbackTransaction();
                    this.log.Error("读取数据库表TRADE_PUBLISH，获得尚未采集图片异常" + commandText, exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

        public DataSet findTopUnSendEmail(int length, int minId)
        {
            string commandText = string.Concat(new object[] { " select  *  from (select * from TRADE_PUBLISH WHERE HAS_SEND_EMAIL = 0 and ID>", minId, " order by  ID ASC )  where ROWNUM<=" + length + " order by ROWNUM asc" });
            DataSet set = new DataSet();
            while (true)
            {
                #region 6点到6点半,8点到18点暂停
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30 || (DateTime.Now.Hour >= 8 && DateTime.Now.Hour < 18 && Convert.ToInt32(DateTime.Now.DayOfWeek) != 0 && Convert.ToInt32(DateTime.Now.DayOfWeek) != 6))
                
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                #endregion
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    set = dbh.ExecuteDataSet( commandText );
                    if (((set.Tables == null) || (set.Tables[0].Rows == null)) || (set.Tables[0].Rows.Count == 0))
                    {
                        this.log.Info("读取数据库表TRADE_PUBLISH，获得尚未发送邮件的2-8类商标0个.");
                        return set;
                    }
                    this.log.Info("读取数据库表TRADE_PUBLISH，获得尚未发送邮件的2-8类商标" + set.Tables[0].Rows.Count + "个.");
                    return set;
                }
                catch (Exception exception)
                {
                    this.log.Error("读取数据库表TRADE_PUBLISH，获得尚未发送邮件的异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }
 
        /// <summary>
        /// 因关闭软件导致的未完成任务。
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public DataSet findUnFinishedTask(int length)
        {
           // string commandText = string.Concat(new object[] { " select * from ( select * from TRADE_PUBLISH WHERE  (HAS_GRASPED = 0 or HAS_ANTI_SEARCH =0 and CATEGORY = 0 or HAS_STATUS_SEARCH=0 and CATEGORY = 0 )  and CLIENT_TASK_NO=", Global.clientNo, "  order by  ISSUE DESC )  where ROWNUM<=" + length + " order by ROWNUM asc" });
            string commandText = string.Concat(new object[] { " select * from ( select * from TRADE_PUBLISH WHERE  (HAS_GRASPED = 2 or HAS_ANTI_SEARCH =2 and CATEGORY = 0  )  and CLIENT_TASK_NO=", Global.clientNo, "  order by  ISSUE DESC,id asc )  where ROWNUM<=" + length  });
            DataSet set = new DataSet();
            while (true)
            {
                #region 6点到6点半,8点到18点暂停
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30 || (DateTime.Now.Hour >= 8 && DateTime.Now.Hour < 18 && Convert.ToInt32(DateTime.Now.DayOfWeek) != 0 && Convert.ToInt32(DateTime.Now.DayOfWeek) != 6))
                
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                #endregion
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    set = dbh.ExecuteDataSet( commandText );
                    if (((set.Tables == null) || (set.Tables[0].Rows == null)) || (set.Tables[0].Rows.Count == 0))
                    {
                        this.log.Info("读取数据库表TRADE_PUBLISH，获得需要本客户端处理的上次未完任务0个.");
                        return set;
                    }
                    this.log.Info("读取数据库表TRADE_PUBLISH，获得需要本客户端处理的上次未完任务" + set.Tables[0].Rows.Count + "个.");
                    return set;
                }
                catch (Exception exception)
                {
                    this.log.Error("读取数据库表TRADE_PUBLISH，获得需要本客户端处理的上次未完任务时发送异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

        public void insertHasAnalyGif(int ISSUE, int category, string regNo, string gif)
        {
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                break;
            }
            if (regNo == null || regNo.Length == 0 || regNo.IndexOf(":") < 0)
            {
                log.Error("$$$$传入的注册号错误" + regNo);
                return;
            }
            string str = this.verifyHas(ISSUE, category, regNo.Split(':')[2]);
            DatabaseHelper dbh = new DatabaseHelper(); 
            try
            { 
                string cmdText = "";
                if (str.Equals("00"))
                {
                    cmdText = "insert into   TRADE_PUBLISH  (id,REG_NO, ISSUE,CATEGORY, HAS_GRASPED,HAS_ANTI_SEARCH,HAS_STATUS_SEARCH,HAS_SEND_EMAIL,FROM_GIF_URL,GIF_ANCHOR) ";
                    object obj2 = cmdText;
                    cmdText = string.Concat(new object[] { obj2, " values(gen_sequence.nextval,'", regNo.Split(':')[2], "', ", ISSUE, ",", category, " ,0,0,0,0,'", gif, "','" + (regNo.Split(':')[0] +":"+ regNo.Split(':')[1]) + "')" });
                }
                else
                {
                    cmdText = string.Concat(new object[] { "update TRADE_PUBLISH  set synchroflag=", str.Substring(str.Length - 1), ",REG_NO='", regNo.Split(':')[2], "', ISSUE=", ISSUE, ",CATEGORY=", category, ", FROM_GIF_URL='", gif, "',GIF_ANCHOR='" + (regNo.Split(':')[0] + ":" + regNo.Split(':')[1]) + "'  where REG_NO='", regNo.Split(':')[2], "' and ISSUE='", ISSUE, "'" });
                }
                dbh.ExecuteNonQuery(cmdText);
                cmdText = null;
            }
            catch (Exception exception)
            {
                throw new GraspTMException("存入TRADE_PUBLISH时,发生异常信息：" + exception.Message);
            }
            finally
            {
                dbh.Dispose();
            } 
        }

        public int saveAntiSearchResult(List<AntiSearchResult> results, List<string[]> details, int tradePublisCls,int tradePublishIssue)
        {
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                break;
            }
            Exception exception;
            if ((results == null) || (results.Count == 0))
            {
                return 0;
            }
            int num = 0;

            #region 先保存反查记录
            while (true)
            {
                DatabaseHelper dbh = new DatabaseHelper();
                string cmdText = "";
                try
                {
                    dbh.BeginTransaction();
                    for (int num2 = 0; num2 < results.Count; num2++)
                    {
                        if (results[num2].needSaveAntiSearch)
                        {
                            String IS_SAME_INTCLS = "0";
                            if(results[num2].detail != null && results[num2].detail[0] != null && results[num2].detail[0].Equals(""+tradePublisCls)){
                                IS_SAME_INTCLS = "1";
                            }
                            String tempflag = "0";
                            if (results[num2].detail != null && results[num2].detail[12] != null && (results[num2].detail[12].Trim().Equals("北京晨光旭通商标代理有限公司") || results[num2].detail[12].Trim().Equals("北京聚集科技有限公司")))
                            {//detail[12 代理人名称;
                                tempflag = "1";
                            }
                            else if (results[num2].detail != null && (results[num2].detail[11] == null || results[num2].detail[11].Trim().Equals("")))
                            {//detail[11] 专用权期限;
                                tempflag = "2";
                            }
                            else if (results[num2].detail != null && results[num2].detail[2] != null && results[num2].detail[2].Trim().Length < 4)
                            {//detail[2] 申请人名称_中文;
                                tempflag = "3";
                            }
                            else if (results[num2].detail != null && results[num2].detail[4] != null && results[num2].detail[4].Trim().Length > 0)
                            {//detail[4] 申请人名称_英文;
                                tempflag = "4";
                            }
                            
                            cmdText = "INSERT INTO  DISSENT_TRADE  (id,TRADE_PUBLISH_ID ,REG_NO,CLASS_NO,TRADE_NAME,ISSUE,ISSUE_DATE,APPLY_USER_NAME,USE_GOODS ,APPLY_DATE,KEY_WORD,SEARCH_TYPE ,SEARCH_CONDITION,HAS_SEND_EMAIL,IS_SAME_INTCLS,TRADE_PUBLISH_ISSUE,tempflag)";
                            object obj2 = cmdText;
                            int issueC = 0;
                            try{
                                issueC =Convert.ToInt32( results[num2].detail[7].Replace("增", "").Replace("]",""));
                            }
                            catch(Exception ex){ 
                            }
                            cmdText = string.Concat(new object[] { 
                        obj2, " VALUES (gen_sequence.nextval,", results[num2].tradePublishId, ",'", results[num2].detail[0x11], "',", results[num2].detail[0], ",'", results[num2].detail[0x12].Replace("'", "''"), "','",issueC, "', "+  (results[num2].detail[8] == null ? "null" :"to_date('"+results[num2].detail[8] +"','yyyy-mm-dd')" ) +",'", results[num2].detail[2].Replace("'", "''"), "',:p1,"+ (results[num2].detail[1] == null ? "null" :"to_date('"+results[num2].detail[1] +"','yyyy-mm-dd')" ) + ",'", results[num2].keyWord.Replace("'", "''"), "','", results[num2].searchType, "','", results[num2].searchCondition, "',0,"+IS_SAME_INTCLS+","+tradePublishIssue+","+tempflag+")"
                     });
                            this.log.Debug("<<<<<<<<" + cmdText);

                            OracleParameter  parameters =  new OracleParameter(":p1", (results[num2].detail[0x15].Replace("'", "''").Length > 1000 ? (results[num2].detail[0x15].Replace("'", "''").Substring(0, 900) + "....<<详见商标详情>>") : results[num2].detail[0x15].Replace("'", "''"))) ;
                            dbh.AddParameter(parameters);
                            dbh.ExecuteNonQuery(cmdText,TradePublish.Service.ConnectionState.KeepOpen);
                            cmdText = null; 
                            num++;
                        }

                    }
                    dbh.CommitTransaction();
                    break;

                }
                catch (Exception exception1)
                {
                    this.log.Error("存入DISSENT_TRADE时,发生异." + cmdText, exception1);
                    dbh.RollbackTransaction();  
                }
                finally
                {
                    dbh.Dispose();
                }
                Thread.Sleep(0x3e8); 
            }
            #endregion

            this.log.Info(string.Concat(new object[] { "针对TRADE_PUBLISH id=", results[0].tradePublishId, "的反查结果共获得", num, "个.已存入DISSENT_TRADE." }));
            BrandInfoDao dao = new BrandInfoDao();
            for (int num2 = 0; num2 < details.Count; num2++)
            {
                try
                {
                    string str2 = dao.verifyHasGrasped(details[num2][0x11]);
                    dao.saveBrandInfo(details[num2], !str2.Substring(0, 1).Equals("0"), Convert.ToInt32(str2.Substring(1)));
                }
                catch (Exception exception2)
                {
                    exception = exception2;
                    this.log.Error("存入BRAND_INFO时,发生异.", exception);
                    Thread.Sleep(0x3e8);
                    num2--;
                } 
            }
            this.log.Info("由反查所引起的更新商标信息共" + results.Count + "个.");
            return num;
        }

        public void setHasAntiSearch(int id, int saveCount)
        {
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    string str3 = string.Concat(new object[] { "update TRADE_PUBLISH set has_grasped=1,HAS_ANTI_SEARCH = 1 ,SEARCH_COUNT=", saveCount, " WHERE ID = ", id });
                    dbh.ExecuteNonQuery(str3 ); 
                    return;
                }
                catch (Exception exception)
                {
                    this.log.Error("保存数据库表TRADE_PUBLISH，设置某公告已反查异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

        public void setHasGraspImage(int id)
        {
            string commandText = "update TRADE_PUBLISH set HAS_GRASP_IMAGE=1  WHERE ID = " + id;
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    dbh.BeginTransaction();
                    dbh.ExecuteNonQuery(commandText, TradePublish.Service.ConnectionState.KeepOpen);
                    dbh.CommitTransaction();
                    return;
                }
                catch (Exception exception)
                {
                    this.log.Error("保存数据库表TRADE_PUBLISH，设置某公告已采集图片异常", exception);
                    dbh.RollbackTransaction();
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

       

        public void setHasStatusSearch(int id)
        {
            string commandText = "update TRADE_PUBLISH set HAS_STATUS_SEARCH = 1 WHERE ID = " + id;
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    dbh.ExecuteNonQuery( commandText );
                    return;
                }
                catch (Exception exception)
                {
                    this.log.Error("保存数据库表TRADE_PUBLISH，设置某公告已状态查询异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

        public int statisticsAntiCount(int issue)
        {
            string commandText = " select count(id)   from TRADE_PUBLISH  where OCR_IDENTIFY_SUCCESS=1 and HAS_ANTI_SEARCH=1 AND CATEGORY=0 AND  ISSUE = " + issue;
            DataSet set = new DataSet();
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    set = dbh.ExecuteDataSet( commandText );
                    if (((set.Tables == null) || (set.Tables[0].Rows == null)) || (set.Tables[0].Rows.Count == 0))
                    {
                        return 0;
                    }
                    return Convert.ToInt32(set.Tables[0].Rows[0][0].ToString());
                }
                catch (Exception exception)
                {
                    this.log.Error("读取数据库表TRADE_PUBLISH，获得已采集个数异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

        public int statisticsCount(int issue)
        {
            string commandText = " select count(id)   from TRADE_PUBLISH  where  OCR_IDENTIFY_SUCCESS=1 and CATEGORY=0 AND  ISSUE = " + issue;
            DataSet set = new DataSet();
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    set = dbh.ExecuteDataSet(  commandText );
                    if (((set.Tables == null) || (set.Tables[0].Rows == null)) || (set.Tables[0].Rows.Count == 0))
                    {
                        return 0;
                    }
                    return Convert.ToInt32(set.Tables[0].Rows[0][0].ToString());
                }
                catch (Exception exception)
                {
                    this.log.Error("读取数据库表TRADE_PUBLISH，获得已采集个数异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

        public int statisticsGraspCount(int issue)
        {
            string commandText = " select count(id)   from TRADE_PUBLISH  where OCR_IDENTIFY_SUCCESS=1 and HAS_GRASPED=1 AND CATEGORY=0 AND  ISSUE = " + issue;
            DataSet set = new DataSet();
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    set = dbh.ExecuteDataSet( commandText );
                    if (((set.Tables == null) || (set.Tables[0].Rows == null)) || (set.Tables[0].Rows.Count == 0))
                    {
                        return 0;
                    }
                    return Convert.ToInt32(set.Tables[0].Rows[0][0].ToString());
                }
                catch (Exception exception)
                {
                    this.log.Error("读取数据库表TRADE_PUBLISH，获得已采集个数异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

        public int statisticsImgCount(int issue)
        {
            string commandText = " select count(id)   from TRADE_PUBLISH  where OCR_IDENTIFY_SUCCESS=1 and HAS_GRASP_IMAGE=1 AND CATEGORY=0 AND  ISSUE = " + issue;
            DataSet set = new DataSet();
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    set = dbh.ExecuteDataSet( commandText );
                    if (((set.Tables == null) || (set.Tables[0].Rows == null)) || (set.Tables[0].Rows.Count == 0))
                    {
                        return 0;
                    }
                    return Convert.ToInt32(set.Tables[0].Rows[0][0].ToString());
                }
                catch (Exception exception)
                {
                    this.log.Error("读取数据库表TRADE_PUBLISH，获得已采集个数异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

        public int statisticsMaxIssue()
        {
            string commandText = " select * from (select ISSUE from REGNO_GIF  order by  ISSUE DESC  )  where ROWNUM<=1 order by ROWNUM asc";
            DataSet set = new DataSet();
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                DatabaseHelper dbh = new DatabaseHelper();
                try
                {
                    set = dbh.ExecuteDataSet(commandText);
                    if (((set.Tables == null) || (set.Tables[0].Rows == null)) || (set.Tables[0].Rows.Count == 0))
                    {
                        return 0;
                    }
                    return Convert.ToInt32(set.Tables[0].Rows[0][0].ToString());
                }
                catch (Exception exception)
                {
                    this.log.Error("读取数据库表REGNO_GIF，获得最大公告期异常", exception);
                    Thread.Sleep(0x1388);
                }
                finally
                {
                    dbh.Dispose();
                } 
            }
        }

        public void updateTradePublish(int id, string[] detial, Boolean isOcrSussess)
        {
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                break;
            }
            DatabaseHelper dbh = new DatabaseHelper();
            try
            {
                String cmd = string.Concat(new object[] { 
                "update  TRADE_PUBLISH   set CLASS_NO='", detial[0], "', TRADE_NAME='", detial[0x12].Replace("'", "''"), "',APPLY_DATE="+(detial[1] == null ?"null":"to_date('"+detial[1]+"','yyyy-mm-dd')")+ ",APPLY_USER_NAME='", detial[2].Replace("'", "''"), "',APPLY_USER_ADDRESS = '", detial[3].Replace("'", "''"), "',ISSUE_DATE = '", detial[8].Replace("'", "''"), "' ,PROXY_USER_NAME = '", detial[12].Replace("'", "''"), 
                "',OCR_IDENTIFY_SUCCESS="+(isOcrSussess?1:0)+", HAS_GRASPED = 1  where   id= ", id
             });
                dbh.ExecuteNonQuery(cmd);
            }
            catch (Exception exception)
            {
                throw new GraspTMException("更新商标公告信息时,发生异常信息：" + exception.Message);
            }
            finally
            {
                dbh.Dispose();
            } 
        }

        public string verifyHas(int id)
        {
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                break;
            }

            string str2;
            string commandText = " select  ID,synchroflag  from TRADE_PUBLISH where id=" + id;
            DataSet set = new DataSet();
            DatabaseHelper dbh = new DatabaseHelper();
            try
            {
                set = dbh.ExecuteDataSet( commandText );
                if ((((set != null) && (set.Tables != null)) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
                {
                    if (Convert.ToInt32(set.Tables[0].Rows[0][1]) == 0)
                    {
                        return (set.Tables[0].Rows[0][0].ToString() + "0");
                    }
                    return (set.Tables[0].Rows[0][0].ToString() + "2");
                }
                str2 = "00";
            }
            catch (Exception exception)
            {
                log.Error(exception.Message);
                throw exception;
            }
            finally
            {
                dbh.Dispose();
            } 
            return str2;
        }

        public string verifyHas(int ISSUE, int category, string regNo)
        {
            while (true)
            {
                if (DateTime.Now.Hour == 6 && DateTime.Now.Minute < 30  || (DateTime.Now.Hour >=8 && DateTime.Now.Hour <18  &&  Convert.ToInt32(DateTime.Now.DayOfWeek) !=0 && Convert.ToInt32(DateTime.Now.DayOfWeek) !=6   ) )
                {
                    Thread.Sleep(180000);//3fenzhong  
                    continue;
                }
                break;
            }
            string str2;
            string commandText = string.Concat(new object[] { " select  ID,synchroflag  from TRADE_PUBLISH where ISSUE=", ISSUE, " and category=", category, " and reg_no='", regNo, "'" });
            DataSet set = new DataSet();
            DatabaseHelper dbh = new DatabaseHelper();
            try
            {
                set = dbh.ExecuteDataSet(commandText);
                if ((((set != null) && (set.Tables != null)) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
                {
                    if (Convert.ToInt32(set.Tables[0].Rows[0][1]) == 0)
                    {
                        return (set.Tables[0].Rows[0][0].ToString() + "0");
                    }
                    return (set.Tables[0].Rows[0][0].ToString() + "2");
                }
                str2 = "00";
            }
            catch (Exception exception)
            {
                log.Error(exception.Message);
                throw exception;
            }
            finally
            {
                dbh.Dispose();
            } 
            return str2;
        }
    }
}

